/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __XOS_COMMON_SRC_OBJECTS_ID_GENERATOR_H__
#define __XOS_COMMON_SRC_OBJECTS_ID_GENERATOR_H__

#include "../tools/head.h"
#include "id_impl.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace xos_common
{

	// 
	// id_impl生成器，考虑已有id_impl,生成顺序的不重复id_impl.可以在四字节数值中轮转(去除了-1, 0, 1三个值)
	// 

	class id_generator_impl : public xos_stl::mem_item< xos::release_impl< id_generator_impl >, thread_lock >,
		public i_id_generator
	{
	public:
		typedef xos::release_impl< id_generator_impl > T;
		typedef id_impl::LIST ID_LIST;
		typedef ID_LIST::ITER ID_ITER;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //

        static int get_xos_item_data( ITEM_DATA*& item_ptr );

    public:
		id_generator_impl();
		~id_generator_impl();

	public:
		int init();
		int term();

	protected:
		xos_set::sets< id_impl, int, int > id_set;
        ID_LIST ids;
		int start;

	public:
		int remove( int mid );
		int add( int mid );
		int gen( int * lpnId );
	};
}

#endif // __XOS_COMMON_SRC_OBJECTS_ID_GENERATOR_H__
